﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WeChatDouble
{
    public partial class SettingWindows : Form
    {
        /// <summary>
        /// 微信安装路径
        /// </summary>
        private string WeChatPath { get; set; }
        /// <summary>
        /// 微信启动文件路径
        /// </summary>
        private string WeChatStartPath { get; set; }
        /// <summary>
        /// 是否已经安装微信
        /// </summary>
        private bool IsInstallWeChat { get; set; }
        /// <summary>
        /// 启动数量
        /// </summary>
        private int StartNum { get; set; }

        public SettingWindows()
        {
            InitializeComponent();
            ReadRegistry();
        }

        #region 事件
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void confirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (!IsInstallWeChat)
                {
                    MessageBox.Show("未检测到安装微信，请先安装微信！", "提示");
                    return;
                }
                if (Common.IsAfterStart("WeChat"))
                {
                    DialogResult dr = MessageBox.Show("检测到您已启动微信，需要关闭后才能多开，是否关闭所有微信？", "提示", MessageBoxButtons.OKCancel);
                    if (dr == DialogResult.OK)
                    {
                        Common.KillProcess("WeChat");
                    }
                    else if (dr == DialogResult.Cancel)
                    {
                        this.Close();
                        return;
                    }
                }

                GetStartNum();
                Start();
                FoundLnk();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("多开异常(可反馈 QQ:3206760335)："+ex.Message);
            }
        }
        /// <summary>
        /// 输入框值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textcount_TextChanged(object sender, EventArgs e)
        {
            if (new Regex("^[1-9]*$").IsMatch(textcount.Text.Trim()) && textcount.Text.Trim().Length > 0)
            {
                DoubleButton.Checked = false;
                ThreeButton.Checked = false;
            }
            else
            {
                DoubleButton.Checked = true;
                ThreeButton.Checked = false;
                textcount.Text = "";
            }
        }
        #endregion

        #region  方法
        /// <summary>
        /// 读取注册表检测是否安装微信,获取微信启动程序路径
        /// </summary>
        /// <returns></returns>
        private void ReadRegistry()
        {
            //D:\Install Software\WeiChatBeta\WeChat\WeChat.exe
            WeChatPath = Common.ReadRegistry("InstallPath", @"SOFTWARE\Tencent\WeChat");
            if (!string.IsNullOrEmpty(WeChatPath))
                WeChatStartPath = WeChatPath + @"\WeChat.exe";

            IsInstallWeChat = string.IsNullOrEmpty(WeChatPath) ? false : true;
        }
        /// <summary>
        /// 获取启动数量
        /// </summary>
        private void GetStartNum()
        {
            if (textcount.Text.Trim().Length > 0)
            {
                StartNum = int.Parse(textcount.Text.Trim());
            }
            else
            {
                StartNum = DoubleButton.Checked ? 2 : 3;
            }
        }

        /// <summary>
        /// 启动
        /// </summary>
        private void Start()
        {
            for (int i = 0; i < StartNum; i++)
            {
                System.Diagnostics.Process.Start(WeChatStartPath);
            }
        }
        /// <summary>
        /// 创建快捷方式
        /// </summary>
        private void FoundLnk()
        {
            //检测.bat
          var aa=  Common.IsFileExist(WeChatPath+@"\微信多开["+ StartNum+@"].bat");

            //检测.lnk
            //桌面路径
          var aas=  Common.IsFileExist(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\微信多开[" + StartNum + @"].lnk");
            //创建.bat

            //创建.lnk


        }

        /// <summary>
        /// 打开已存在的快捷方式
        /// </summary>
        /// <returns></returns>
        //private bool GetAlreadyExistPath()
        //{
        //    //桌面路径
        //    string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        //    string[] path = Directory.GetFiles(desktopPath, "*.lnk");

        //    if (path != null && path.Length > 0)
        //    {
        //        foreach (var item in path)
        //        {
        //            string name = Path.GetFileName(item);
        //            if (!name.Contains("微信") || !name.Contains(".lnk")) continue;

        //            string FileTargetPath = Common.GetFileTargetPath(item);
        //            string FileTargetname = Path.GetFileName(FileTargetPath);
        //            if (FileTargetname.Contains(".bat") && DoubleButton.Checked && FileTargetname.Contains("2"))
        //            {
        //                System.Diagnostics.Process.Start(FileTargetPath);
        //                return true;
        //            }
        //            if (FileTargetname.Contains(".bat") && ThreeButton.Checked && FileTargetname.Contains("3"))
        //            {
        //                System.Diagnostics.Process.Start(FileTargetPath);
        //                return true;
        //            }
        //            if (FileTargetname.Contains(".bat") && textcount.Text.Trim().Length > 0 && FileTargetname.Contains(textcount.Text.Trim()))
        //            {
        //                System.Diagnostics.Process.Start(FileTargetPath);
        //                return true;
        //            }
        //        }
        //    }
        //    return false;
        //}
        #endregion

    }
}
